package com.wzh.service;

import com.wzh.dao.CardDao;
import com.wzh.dao.UserDao;


import com.wzh.pojo.Po.*;
import com.wzh.pojo.Req.DataReq;
import com.wzh.pojo.Resp.DataResp;
import com.wzh.util.CommmonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class Userser {
	@Autowired
	private UserDao userdao;
	@Autowired
	private CardDao cardDao;
	public List<User> get(){
		return userdao.getAll();
	};

	public boolean delete(User user){
		return userdao.deonebyid(user)>0;
	}
	public User login(User user) {
		return userdao.login(user);
	}
	public Boolean addmoney(User user) {
		int res=userdao.Sendrecoed(user);
		return userdao.addmoney(user)>0;
	}

	public boolean register(User user2) {
		boolean res=false;
		if (userdao.findUserid(user2)==null){
			if (userdao.register(user2)>0){
				res=true;
			}
		}
		return res;
	}



	public List<NameList> readNameList() {

		return userdao.readList();
	}

	public DataResp<Object> show(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<User> list= userdao.findall(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}
    @Transactional
	public boolean delCoin(User user2) {

		User user =userdao.findUserid(user2);
		if(user.getMoney()<user2.getMoney()){
			return false;
		}

		return userdao.delmoney(user2)>0;
	}

	public DataResp<Object> sendCarhRecord(Cash cash) {
		DataResp<Object> resp=new DataResp<>();
		cash.setCashId(CommmonUtil.getUUID());
		cash.setTime(CommmonUtil.getNowTime());
		Cash  record= userdao.findCashRecord(cash);
		if (record==null){
			int rescode=userdao.sendCashRecord(cash);
			if (rescode>0){
				resp.setMes("提交成功！请耐心等待");
			}else{
				resp.setMes("提交失败！");
			}
		}else{
			resp.setMes("你有一条提现申请正在审核...请耐心等待");
		}
		return  resp;
	}
@Transactional
	public DataResp<Object> checkCarhRecord(Cash cash) {
		DataResp<Object> resp=new DataResp<>();
		Integer res = userdao.checkCashRecord(cash);
		if (res!=0){
			//修改状态
			User user =new User();
			user.setMoney(cash.getMoney());
			user.setUserid(cash.getIphone());
			boolean delCoinRes=false;
			//打款才扣余额
			if(cash.getStatus()==1){
				delCoinRes = delCoin(user);
			}
			if (delCoinRes){
				resp.setMes("操作成功！");
			}else {
				resp.setMes("操作失败！");
			}
		}else{
			resp.setMes("操作失败！");
		}

		return  resp;
	}

	public DataResp getCashRecord(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<Cash> list= userdao.getCashRecord(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}


	public DataResp<Object> getMoneyRecord(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<Cash> list= userdao.getMoneyRecord(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}

	public boolean boundDev(User user) {
		return userdao.boundDev(user)>0;
	}

	public boolean DelDev(User user) {
		return userdao.DelDev(user)>0;
	}

	public boolean UPdateVipTime(User user) {
		return userdao.UPdateVipTime(user)>0;
	}

	public DataResp<Object> delUser(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		int res= userdao.delUser(req);
		if(res!=0){
			resp.setMes("操作成功！");
		}else{
			resp.setMes("操作成功！");
		}
		return  resp;
	}
}
